/*<!--
@name:
@description:
@author: lzy
@title: 天王盖地虎!!!
@time:  2021-01-11 15:12:45
-->*/
package controller;

import dao.BaseDao;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    BaseDao baseDao=new BaseDao();
    User user=new User();
    PreparedStatement pstm=null;
    ResultSet rs=null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        user.setId(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        String sql="SELECT * FROM chezhuinfo WHERE id=? AND LoginPassWord=?;";
        Object [] objs={user.getId(),user.getPassword()};
        pstm=baseDao.pstmt(sql,objs);
        try {
            rs = pstm.executeQuery();
            if (rs.next()){
                req.setAttribute("username",rs.getString(3));
                req.getSession().setAttribute("user",user);
                req.getRequestDispatcher("/mianMenu.jsp").forward(req,resp);
            }else {
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
